//
//  RZQSeacherViewController.m
//  LastOne
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQSeacherViewController.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "RZQFindWebViewController.h"
#import "CirleModel.h"
#import "RZQDataCollectionViewCell.h"
#import <MJRefresh.h>
#import "NightHeader.h"
#import "GiFHUD.h"
@interface RZQSeacherViewController ()<UISearchBarDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UISearchBar *searchBar;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)UIView *noThingView;
@property(nonatomic, strong)NSString *string;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)NSNumber *page;
@end

@implementation RZQSeacherViewController
-(void)viewWillAppear:(BOOL)animated
{
      self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _searchBar = [[UISearchBar alloc]init];
    _searchBar.frame = CGRectMake(0, 0, ScreenWidth, 60 * FitHeight);
    _searchBar.placeholder = @"寻找你好奇的一切";
    _searchBar.delegate = self;
    [self.view addSubview:_searchBar];
    _noThingView = [[UIView alloc]initWithFrame:CGRectMake(0, 60 * FitHeight, ScreenWidth, ScreenHeight)];
    
    
    
    [self.view addSubview:_noThingView];
    [GiFHUD setGifWithImageName:@"loading.gif"];
    
    // Do any additional setup after loading the view.
}
#pragma mark 搜索关键字
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    _noThingView.hidden = YES;

    _string = [NSString stringWithFormat:@"%@", searchBar.text];
       _page = [NSNumber numberWithInt:0];
    self.view.backgroundColor = [UIColor whiteColor];
    _dataSource = [NSMutableArray array ];
    
    [self getNet];
    
    UICollectionViewFlowLayout *dataFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    dataFlowLayout.minimumInteritemSpacing = 5 ;
    dataFlowLayout.minimumLineSpacing = 5;
    dataFlowLayout.sectionInset = UIEdgeInsetsMake(5 , 5 , 5, 5 );
    _dataSource = [NSMutableArray array ];
   
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 113 * FitHeight) collectionViewLayout:dataFlowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView NightWithType:UIViewColorTypeNormal];
    [_collectionView registerClass:[RZQDataCollectionViewCell class] forCellWithReuseIdentifier:@"rzqIdentifier"];
     _collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self getNet];
        
    } ];
    
    [searchBar resignFirstResponder];
    
    
    
    
}
#pragma mark 网络请求
-(void)getNet
{
    [GiFHUD show];
    [HTTPMethod getDataByString:[NSString stringWithFormat:@"http://app.qdaily.com/app/searches/post_list?last_time=%@&search=%@", _page, _string] BodyDic:nil WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"response"];
        NSDictionary *Dic = [dic objectForKey:@"searches"];
        _page = [Dic objectForKey:@"last_time"];
        NSArray *array = [Dic objectForKey:@"list"];
        for (NSDictionary *dic  in array) {
            NSDictionary *post = [dic objectForKey:@"post"];
            CirleModel *model = [[CirleModel alloc]init];
            [model setValuesForKeysWithDictionary:post];
            [_dataSource addObject:model];
         
        }

        [_collectionView reloadData];
        [_collectionView.header endRefreshing];
        [GiFHUD dismiss];
    }];
    
    
    
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return  CGSizeMake(ScreenWidth / 2 - 10 * FitWidth  , 200 * FitHeight);
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RZQDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"rzqIdentifier" forIndexPath:indexPath];
    cell.cirData = _dataSource[indexPath.row];
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = YES;
    RZQFindWebViewController *rzq = [[RZQFindWebViewController alloc]init];
    [self.navigationController pushViewController:rzq animated:YES];
    CirleModel *model = [[CirleModel alloc]init];
    model = _dataSource[indexPath.row];
    rzq.modelData = model;
    
    self.hidesBottomBarWhenPushed = NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
